package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName AdminUserServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/7/22 11:25
 * @Version V1.0
 **/
public class AdminUserServiceImpl implements AdminUserService {
    @Override
    public List<MarketUser> list(String page, String limit, String sort, String order, String username, String mobile) {
        SqlSession session = MybatisUtil.getSession();
        MarketUserMapper userMapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            // select * from sxxx where username like '%xxx%'
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(mobile)){
            criteria.andMobileLike("%" + mobile + "%");
        }
        // add_time desc  select xxx from xx where xxx order by xxx limit xx,xxx
        //使用分页插件来键分页非常简单，如果不添加分页插件的代码，那么我们查询的便是全部数据
        //如果添加了分页插件的代码，那么查询出来的便是分页之后的数据
        //两个参数分别是当前页、每页的数量
        marketUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        //每页10条记录  1页  2页
        //如果使用分页插件，那么我们得到的list是可以强转成一个page对象，该对象内部会包含上述所需要的total、pages、limit、page等属性
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        session.commit();
        session.close();
        return marketUsers;
    }
}
